package spittr.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@Configuration
/*
 * Enable Spring MVC
 */
@EnableWebMvc
/*
 * with @Controller in package spittr.web
 * you won’t have to explicitly declare any controllers in this configuration class.
 */
@ComponentScan("spittr.web")
public class WebConfig extends WebMvcConfigurerAdapter {

    /**
     * Configure a JSP view resolver
     */
    @Bean
    public ViewResolver viewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".jsp");
        // 使所有应用程序上下文中的bean可通过JSTL表达式来访问
        resolver.setExposeContextBeansAsAttributes(true);
        return resolver;
    }

    /**
     * Configure static content handling
     * 已过时
     */
    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        // Asking DispatcherServlet to forward requests for static resources
        // to the servlet container’s default servlet and not to try to handle them itself.
        configurer.enable();
    }
}
